package com.mobile.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.mobile.pojo.LoginDTO;
import com.mobile.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Encounter
 * @date 2024/10/15 15:05<br/>
 */
@RestController
@RequestMapping("/member")
@Slf4j
public class MemberController
    {
        @Autowired
        private MemberService memberService;
        
        /**
         * 登录
         *
         * @param loginDTO 登录 DTO
         * @return {@link Result }
         */
        @PostMapping("/login")
        public Result login(@RequestBody LoginDTO loginDTO)
            {
                try
                    {
                        log.info("登录信息：{}", loginDTO);
                        boolean flag = memberService.login(loginDTO);
                        if (flag)
                            {
                                return new Result(true, MessageConstant.LOGIN_SUCCESS);
                            }
                        else
                            {
                                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
                            }
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        log.error("登陆失败");
                        return ResponseEntity.badRequest().body(new Result(false, e.getMessage())).getBody();
                    }
            }
    }
